<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: class.wish_list.php 483 2014-01-30 16:23:29Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 483 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2014-01-30 16:23:29 +0000 (Thu, 30 Jan 2014) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require_once(DIR_WS_CLASSES.'class.cart.php');

class wish_list extends cart {
	var $contents, $total, $weight, $cartID, $content_type;
	var $dbTable;
	
	function __construct () {
		$this->dbTable = 'WISHLIST';
		$this->reset();
		$this->restore_contents();
	}
	
	function all_to_cart(){
		$contents = $this->get_products();
		if(is_array($contents)) {
			foreach ($contents AS $content) {
				if ($content['quantity'] > MAX_PRODUCTS_QTY)
					$content['quantity'] = MAX_PRODUCTS_QTY;

				$_SESSION['cart']->add_cart($content['id'], remove_non_numeric($content['quantity']), ($content['attributes'] ? $content['attributes'] : ''), false);
				$this->remove($content['id']);
			}
		}
	}

	/**
	 * aktuelle Werte in die Datenbank schreiben
	 *
	 * @return unknown
	 */

	function dep_restore_contents() {
		global $db;

		if(!isset($_SESSION['customer_id']))
			return false;

		if (is_array($this->contents)) {
			reset($this->contents);

			while(list($products_id, ) = each($this->contents)) {
				$qty = $this->contents[$products_id]['qty'];
				$product = $db->db_query("SELECT
												products_id
											FROM
												".TABLE_CUSTOMERS_WISHLIST."
											WHERE
												customers_id = '".$_SESSION['customer_id']."'
											AND
												products_id = '".$products_id."'");

				if($product->_numOfRows <= 0) {
					$db->db_query("INSERT INTO
											".TABLE_CUSTOMERS_WISHLIST."
												(customers_id, products_id, customers_basket_quantity, customers_basket_date_added)
										VALUES
											('".$_SESSION['customer_id']."', '".$products_id."', '".$qty."', '".date('Ymd')."')");
					if(isset($this->contents[$products_id]['attributes'])) {
						reset($this->contents[$products_id]['attributes']);
						while(list($option, $value) = each($this->contents[$products_id]['attributes'])) {
						$db->db_query("INSERT INTO
											".TABLE_CUSTOMERS_WISHLIST_ATTRIBUTES."
											(customers_id, products_id, products_options_id, products_options_value_id)
										VALUES
											('".$_SESSION['customer_id']."', '".$products_id."', '".$option."', '".$value."')");
						}
					}
				} else
					$db->db_query("UPDATE
										".TABLE_CUSTOMERS_WISHLIST."
									SET
										customers_basket_quantity = '".$qty."'
									WHERE
										customers_id = '".(int)$_SESSION['customer_id']."'
									AND
										products_id = '".$products_id."'");

			}
		}

		$this->reset(false);

		$products = $db->db_query("SELECT
										products_id,
										customers_basket_quantity
									FROM
										".TABLE_CUSTOMERS_WISHLIST."
									WHERE
										customers_id = '".(int)$_SESSION['customer_id']."'");
		while(!$products->EOF) {
			$this->contents[$products->fields['products_id']] = array('qty' => $products->fields['customers_basket_quantity']);

			// Attribute
			$attributes = $db->db_query("SELECT
													products_options_id,
													products_options_value_id
												FROM
													".TABLE_CUSTOMERS_WISHLIST_ATTRIBUTES."
												WHERE
													customers_id = '".$_SESSION['customer_id']."'
												AND
													products_id = '".$products->fields['products_id']."'");
			while(!$attributes->EOF) {
				$this->contents[$products->fields['products_id']]['attributes'][$attributes->fields['products_options_id']] = $attributes->fields['products_options_value_id'];
				$attributes->MoveNext();
			}
			$products->MoveNext();
		}

		$this->cleanup();
	}

	/**
	 * Löschen des temporären Merkzettels /
	 * Löschen der Merkzettel-Session
	 *
	 * @param unknown_type $reset_database
	 */
	 
	function reset($reset_database = false) {
		global $db;

		$this->contents = array();
		$this->total = 0;
		$this->weight = 0;
		$this->content_type = false;

		if(isset($_SESSION['customer_id']) && ($reset_database == true)) {
			$db->db_query("DELETE FROM ".TABLE_CUSTOMERS_WISHLIST." WHERE customers_id = '".(int)$_SESSION['customer_id']."'");
			$db->db_query("DELETE FROM ".TABLE_CUSTOMERS_WISHLIST_ATTRIBUTES." WHERE customers_id = '".(int)$_SESSION['customer_id']."'");
		}

		unset($this->cartID);
		if(isset($_SESSION['cartID']))
			unset($_SESSION['cartID']);
	}

	/**
	 * Produkt und Attribut zum Merkzettel hinzufügen
	 *
	 * @param $string $products_id
	 * @param numeric $qty
	 * @param array $attributes
	 * @param bolean $notify
	 */

	function dep_add_cart($products_id, $qty = 1, $attributes = '', $notify = true) {
		global $new_products_id_in_cart, $db;

		$products_id = get_uprid($products_id, $attributes);
		if($notify == true)
			$_SESSION['new_products_id_in_cart'] = $products_id;

		if($this->in_cart($products_id))
			$this->update_quantity($products_id, $qty, $attributes);

		else {
			$this->contents[] = array($products_id);
			$this->contents[$products_id] = array('qty' => $qty);
			if(isset($_SESSION['customer_id']))
				$db->db_query("INSERT INTO ".TABLE_CUSTOMERS_WISHLIST." (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) VALUES ('".$_SESSION['customer_id']."', '".$products_id."', '".$qty."', '".date('Ymd')."')");

			if(is_array($attributes)) {
				reset($attributes);
				while(list($option, $value) = each($attributes)) {
					$this->contents[$products_id]['attributes'][$option] = $value;
					if(isset($_SESSION['customer_id']))
						$db->db_query("INSERT INTO ".TABLE_CUSTOMERS_WISHLIST_ATTRIBUTES." (customers_id, products_id, products_options_id, products_options_value_id) VALUES ('".$_SESSION['customer_id']."', '".$products_id."', '".$option."', '".$value."')");
				}
			}
		}
		$this->cleanup();
		$this->cartID = $this->generate_cart_id();
	}

	/**
	 * erhöhen/vermindern der Menge eine Artikles im Merkzettel
	 *
	 * @param unknown_type $products_id
	 * @param unknown_type $quantity
	 * @param unknown_type $attributes
	 * @return unknown
	 */
	function update_quantity($products_id, $quantity = '', $attributes = '') {
		global $db;

		if(empty($quantity))
			return true;

		$this->contents[$products_id] = array('qty' => $quantity);

		if(isset($_SESSION['customer_id'])) {
			$db->db_query("UPDATE 
								".TABLE_CUSTOMERS_WISHLIST." 
							SET 
								customers_basket_quantity = '".$quantity."' 
							WHERE 
								customers_id = '".(int)$_SESSION['customer_id']."' 
							AND 
								products_id = '".$products_id."'");

			if(is_array($attributes)) {
				reset($attributes);
				while(list($option, $value) = each($attributes)) {
					$this->contents[$products_id]['attributes'][$option] = $value;
	
					if(isset($_SESSION['customer_id']))
					$db->db_query("UPDATE 
										".TABLE_CUSTOMERS_WISHLIST_ATTRIBUTES." 
									SET 
										products_options_value_id = '".$value."' 
									WHERE 
										customers_id = '".(int)$_SESSION['customer_id']."' 
									AND 
										products_id = '".$products_id."' 
									AND 
										products_options_id = '".$option."'");
				}
			}
		}
	}

	function cleanup() {
		global $db;

		reset($this->contents);
		while (list($key,) = each($this->contents)) {
			if ($this->contents[$key]['qty'] < 1) {
				unset($this->contents[$key]);
				if (isset($_SESSION['customer_id'])) {
					$db->db_query("DELETE FROM ".TABLE_CUSTOMERS_WISHLIST." WHERE customers_id = '".$_SESSION['customer_id']."' AND products_id = '".$key."'");
					$db->db_query("DELETE FROM ".TABLE_CUSTOMERS_WISHLIST_ATTRIBUTES." WHERE customers_id = '".$_SESSION['customer_id']."' AND products_id = '".$key."'");
				}
			}
		}
	}

	/**
	 * Durchzählen der Merkzettel Items
	 *
	 * @return unknown
	 */
	function dep_count_contents() {
		$total_items = 0;
		if(is_array($this->contents)) {
			reset($this->contents);
			while(list($products_id, ) = each($this->contents))
				$total_items += $this->get_quantity($products_id);
		}
		return $total_items;
	}

	/**
	 * Menge des Artikels ermitteln
	 *
	 * @param unknown_type $products_id
	 * @return numeric
	 */
	function get_quantity($products_id) {
		if(isset($this->contents[$products_id]))
			return $this->contents[$products_id]['qty'];
		else
			return 0;

	}

	/**
	 * Prüfen ob Artikel auf Merkzettel
	 *
	 * @param unknown_type $products_id
	 * @return unknown
	 */
	function in_cart($products_id) {
		if (isset($this->contents[$products_id]))
			return true;
		else
			return false;
	}

	/**
	 * Artikel vom Merkzettel löschen
	 *
	 * @param unknown_type $products_id
	 */
	function dep_remove($products_id) {
		global $db;

		unset($this->contents[$products_id]);
		if(isset($_SESSION['customer_id'])) {
			$db->db_query("DELETE FROM ".TABLE_CUSTOMERS_WISHLIST." WHERE customers_id = '".$_SESSION['customer_id']."' AND products_id = '".$products_id."'");
			$db->db_query("DELETE FROM ".TABLE_CUSTOMERS_WISHLIST_ATTRIBUTES." WHERE customers_id = '".$_SESSION['customer_id']."' AND products_id = '".$products_id."'");
		}
		$this->cartID = $this->generate_cart_id();
	}

	/**
	 * Produkte vom Merzettel löschen
	 *
	 */
	function remove_all() {
		$this->reset();
	}

	function get_product_id_list() {
		$product_id_list = '';
		if(is_array($this->contents)) {
			reset($this->contents);
			while(list($products_id, ) = each($this->contents))
				$product_id_list .= ', '.$products_id;
		}

		return substr($product_id_list, 2);
	}

	public function calculate() {
    	global $price, $db;
	    $this->total = 0;
	    $this->weight = 0;
	    $this->tax = array ();
	    $this->total_discount = array ();
	    if (!is_array($this->contents))
	    	return 0;
	    reset($this->contents);
	    while (list ($products_id) = each($this->contents)) {
	      	$qty = $this->contents[$products_id]['qty'];
			$product = $db->db_query("SELECT
											products_id,
											products_price,
											products_discount_allowed,
											products_tax_class_id,
											products_weight
										FROM
											".TABLE_PRODUCTS."
										WHERE
											products_id='".get_prid($products_id)."'");

			if($product->_numOfRows) {
				
				if(($_price = $price->GetGroupPrice($product->fields['products_id'], $qty)) && ($_SESSION['customers_status']['customers_status_graduated_prices'] == 1))
					$single_q = $qty;
				else
					$single_q = 1;
				
				$products_price = $price->GetPrice($product->fields['products_id'], false, $single_q, $product->fields['products_tax_class_id'], $product->fields['products_price']);
				$this->total += ($products_price * $qty);
				$this->weight += ($qty * $product->fields['products_weight']);
				$attribute_price = 0;
				if(isset($this->contents[$products_id]['attributes'])) {
					reset($this->contents[$products_id]['attributes']);
					while(list($option, $value) = each($this->contents[$products_id]['attributes'])) {
						$values = $price->GetOptionPrice($product->fields['products_id'], $option, $value);
						$this->weight 		+= $values['weight'] * $qty;
						$this->total 		+= $values['price'] * $qty;
						$attribute_price 	+= $values['price'];
					}
				}

				if($product->fields['products_tax_class_id'] != 0) {
					if ($_SESSION['customers_status']['customers_status_ot_discount_flag'] == 1) {
						$products_price_tax = $products_price - ($products_price / 100 * $_SESSION['customers_status']['customers_status_ot_discount']);
						$attribute_price_tax = $attribute_price - ($attribute_price / 100 * $_SESSION['customers_status']['customers_status_ot_discount']);
					}
					$products_tax = $price->TAX[$product->fields['products_tax_class_id']];
					$products_tax_description = get_tax_description($product->fields['products_tax_class_id']);

					// Preis inkl. MwSt.
					if ($_SESSION['customers_status']['customers_status_show_price_tax'] == '1') {
						if ($_SESSION['customers_status']['customers_status_ot_discount_flag'] == 1) {
							$this->tax[$product->fields['products_tax_class_id']]['value'] += ((($products_price_tax+$attribute_price_tax) / (100 + $products_tax)) * $products_tax)*$qty;
							$this->tax[$product->fields['products_tax_class_id']]['desc'] = TAX_ADD_TAX."$products_tax_description";
						} else {
							$this->tax[$product->fields['products_tax_class_id']]['value'] += ((($products_price+$attribute_price) / (100 + $products_tax)) * $products_tax)*$qty;
							$this->tax[$product->fields['products_tax_class_id']]['desc'] = TAX_ADD_TAX."$products_tax_description";
						}
					}
					// Preis exkl. MwSt. + tax at checkout
					if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 0 && $_SESSION['customers_status']['customers_status_add_tax_ot'] == 1) {
						if ($_SESSION['customers_status']['customers_status_ot_discount_flag'] == 1) {
							$this->tax[$product->fields['products_tax_class_id']]['value'] += (($products_price_tax+$attribute_price_tax) / 100) * ($products_tax)*$qty;
							$this->total_discount[$product->fields['products_tax_class_id']]+=(($products_price_tax+$attribute_price_tax) / 100) * ($products_tax)*$qty;
							$this->tax[$product->fields['products_tax_class_id']]['desc'] = TAX_NO_TAX."$products_tax_description";
						} else {
							$this->tax[$product->fields['products_tax_class_id']]['value'] += (($products_price+$attribute_price) / 100) * ($products_tax)*$qty;
							$this->total_discount[$product->fields['products_tax_class_id']]+=(($products_price+$attribute_price) / 100) * ($products_tax)*$qty;
							$this->tax[$product->fields['products_tax_class_id']]['desc'] = TAX_NO_TAX."$products_tax_description";
						}
					}
				}
			}
		}
		foreach ($this->total_discount as $value) {
	      $this->total+=round($value, $price->get_decimal_places($order->info['currency']));
	    }
	}

	function dep_attributes_price($products_id) {
		global $price, $db;

		if(isset($this->contents[$products_id]['attributes'])) {
			reset($this->contents[$products_id]['attributes']);
			while(list($option, $value) = each($this->contents[$products_id]['attributes'])) {
				$attribute_price = $db->db_query("SELECT
														pd.products_tax_class_id,
														p.options_values_price,
														p.price_prefix
													FROM
														".TABLE_PRODUCTS_ATTRIBUTES." p,
														".TABLE_PRODUCTS." pd
													WHERE
														p.products_id = '".$products_id."'
													AND
														p.options_id = '".$option."'
													AND
														pd.products_id = p.products_id
													AND
														p.options_values_id = '".$value."'");

				if($attribute_price->fields['price_prefix'] == '+')
					$attributes_price += $price->format($attribute_price->fields['options_values_price'], false, $attribute_price->fields['products_tax_class_id']);
				else
					$attributes_price -= $price->format($attribute_price->fields['options_values_price'], false, $attribute_price->fields['products_tax_class_id']);
			}
		}
		return $attributes_price;
	}

	function dep_get_products() {
		global $price, $main, $db, $product;

		if(!is_array($this->contents))
			return false;
			
		if(is_object($product))	
			global $product;
		else 
			$product = new product();

		$products_array = array();
		reset($this->contents);
		while(list($products_id, ) = each($this->contents)) {
			$products = $db->db_query("SELECT
											p.products_id,
											pd.products_name,
											p.products_image,
											p.products_model,
											p.products_price,
											p.products_discount_allowed,
											p.products_weight,
											p.products_vpe_status,
											p.products_vpe,
											p.products_vpe_value,
											p.products_tax_class_id
										FROM
											".TABLE_PRODUCTS." p,
											".TABLE_PRODUCTS_DESCRIPTION." pd
										WHERE
											p.products_id = '".get_prid($products_id)."'
										AND
											pd.products_id = p.products_id
										AND
											pd.language_id = '".$_SESSION['languages_id']."'");

			if($products->_numOfRows) {
				$prid = $products->fields['products_id'];

				$products_price = $price->GetPrice($products->fields['products_id'], false, $this->contents[$products_id]['qty'], $products->fields['products_tax_class_id']);
				$products_price_single = $price->GetPrice($products->fields['products_id'], false, 1, $products->fields['products_tax_class_id']);
				
				$products_array[] = array(	'id' => $products_id,
											'name' => $products->fields['products_name'],
											'model' => $products->fields['products_model'],
											'image' => $product->productImage($products->fields['products_image'], 'icon'),
											'price' => $products_price + $this->attributes_price($products_id),
											'single_price' => $products_price_single,
											'quantity' => $this->contents[$products_id]['qty'],
											'weight' => $products->fields['products_weight'],
											'shipping_time' => $main->getShippingStatusName($products->fields['products_shippingtime']),
											'final_price' => ($products_price + $this->attributes_price($products_id)),
											'tax_class_id' => $products->fields['products_tax_class_id'],
											'attributes' => $this->contents[$products_id]['attributes']);
			}
		}

		return $products_array;
	}

	function show_total() {
		$this->calculate();
		return $this->total;
	}

	function show_weight() {
		$this->calculate();
		return $this->weight;
	}

	function generate_cart_id($length = 5) {
		return create_random_value($length, 'digits');
	}

	function get_content_type() {
		global $db;

		$this->content_type = false;

		if((DOWNLOAD_ENABLED == 'true') && ($this->count_contents() > 0)) {
			reset($this->contents);
			while(list($products_id, ) = each($this->contents)) {
				if(isset($this->contents[$products_id]['attributes'])) {
					reset($this->contents[$products_id]['attributes']);
					while(list(, $value) = each($this->contents[$products_id]['attributes'])) {
						$virtual_checky = $this->db->db_query("SELECT
																count(*) AS total
															FROM
																".TABLE_PRODUCTS_ATTRIBUTES." pa,
																".TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD." pad
															WHERE
																pa.products_id = '".$products_id."'
															AND
																pa.options_values_id = '".$value."'
															AND
																pa.products_attributes_id = pad.products_attributes_id");

						if($virtual_check->fields['total'] > 0) {
							switch ($this->content_type) {
								case 'physical':
									$this->content_type = 'mixed';
									return $this->content_type;
									break;

								default:
									$this->content_type = 'virtual';
									break;
							}
						} else {
							switch ($this->content_type) {
								case 'virtual':
									$this->content_type = 'mixed';
									return $this->content_type;
									break;

								default:
									$this->content_type = 'physical';
									break;
							}
						}
					}
				} else {
					switch ($this->content_type) {
						case 'virtual':
							$this->content_type = 'mixed';
							return $this->content_type;
							break;

						default:
							$this->content_type = 'physical';
							break;
					}
				}
			}
		} else
			$this->content_type = 'physical';

		return $this->content_type;
	}

	function unserialize($broken) {
		for(reset($broken);$kv=each($broken);) {
			$key=$kv['key'];
			if(gettype($this->$key) != "user function")
				$this->$key=$kv['value'];
		}
	}

	function count_contents_virtual() {
		global $db;

		$total_items = 0;
		if(is_array($this->contents)) {
			reset($this->contents);
			while(list($products_id, ) = each($this->contents)) {
				$no_count = false;
				$gv_result = $this->db->db_query("SELECT products_model FROM ".TABLE_PRODUCTS." WHERE products_id = '".$products_id."'");

				if(preg_match('/^GIFT/', $gv_result->fields['products_model']))
					$no_count = true;

				if(NO_COUNT_ZERO_WEIGHT == 1) {
					$gv_result = $this->db->db_query("SELECT products_weight FROM ".TABLE_PRODUCTS." WHERE products_id = '".get_prid($products_id)."'");
					if($gv_result->fields['products_weight']<=MINIMUM_WEIGHT)
						$no_count = true;
				}
				if(!$no_count)
					$total_items += $this->get_quantity($products_id);
			}
		}
		return $total_items;
	}
}